package com.ruoyi.web.controller.system;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.City;
import com.ruoyi.common.core.domain.entity.Institution;
import com.ruoyi.common.core.domain.entity.Smalluser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.service.IInstitutionService;
import com.ruoyi.system.service.ISysProvincesService;
import com.ruoyi.system.service.InstitutionUserService;
import com.ruoyi.system.service.impl.InstitutionUserServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/*
* 机构de用户管理
*
* @author lc
* */
@Controller
@RequestMapping("/itsystem/smalluser")
public class InstUserController extends BaseController {

    @Autowired
    private InstitutionUserService userService;
    @Autowired
    private IInstitutionService institutionService;
    @Autowired
    private ISysProvincesService provincesService;

    /*跳转路径前缀*/
    private String prefix = "itsystem/smalluser";

    @GetMapping()
    @RequiresPermissions("itsystem:smalluser:view")
    public String smalluser(ModelMap modelMap){
        modelMap.addAttribute("citys",provincesService.selectAllCity());
        return prefix + "/smalluser";
    }

    //    查询所有用户信息
//    @RequestMapping("/list/{itid}")
//    @RequiresPermissions("itsystem:smalluser:list")
//    @ResponseBody
//    public TableDataInfo selectUserLis(@PathVariable("itid") long itId){
//        List<Smalluser> list = userService.selectUserLis(itId);
//        return getDataTable(list);
//    }
    @PostMapping("/list")
    @RequiresPermissions("itsystem:smalluser:list")
    @ResponseBody
    public TableDataInfo selectUserLis(Smalluser smalluser){
        //获取当前登录用户的id
        Long uId = ShiroUtils.getSysUser().getuId();
        Institution institution = institutionService.queryByUid(uId);
        //获取当前用户所关联的机构
        Long itId = institution.getItId();
        //查询该机构的所有用户
        List<Smalluser> list1 = userService.selectUserList(itId);
        List<Smalluser> list2 = null;
        List<Smalluser> list = list1;
        //多属性查询机构用户
        if(smalluser.getSuName()!=""||smalluser.getSuPhone()!=""||
            smalluser.getSuProvince()!=""||smalluser.getSuCity()!=""
            ||smalluser.getSuArea()!=""||smalluser.getTime()!=null){
            list2 = userService.selectUser(smalluser);
            list = new ArrayList<Smalluser>();
            for (int i = 0; i < list1.size(); i++)
            {
                for(int j = 0; j < list2.size(); j++)
                {
                    if(list1.get(i).getSuId()==list2.get(j).getSuId())
                    {
                        list.add(list1.get(i));
                        break;
                    }
                }
            }
        }
        return getDataTable(list);
    }


//    添加用户
    @GetMapping("/add")
    @RequiresPermissions("itsystem:smalluser:add")
    public String add(ModelMap modelMap){
        List<City> citys = provincesService.selectAllCity();
        modelMap.addAttribute("citys", citys);
        return prefix + "/add";
    }

    @PostMapping("/add")
    @ResponseBody
    @RequiresPermissions("itsystem:smalluser:add")
    @Log(title = "机构用户管理", businessType = BusinessType.INSERT)
    public AjaxResult addItUser(@Validated Smalluser user){
        user.setSuId(ShiroUtils.getUserId().longValue());
        if(userService.checkPhoneUnique(user))
        {
            return toAjax(userService.addItUser(user));
        }
        else
            return error("新增机构用户'"+ user.getSuName() +"'失败，联系方式已存在");

    }

    @GetMapping("/edit/{suId}")
    @RequiresPermissions("itsystem:smalluser:edit")
    public String editItUser(@PathVariable("suId") Long suId, ModelMap modelMap){
        List<City> citys = provincesService.selectAllCity();
        modelMap.addAttribute("citys", citys);
        modelMap.put("smalluser",userService.querySmalleruser(suId));
        return prefix + "/edit";
    }


    //    修改用户
    @PostMapping("/edit")
    @ResponseBody
    @Log(title = "机构用户管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("itsystem:smalluser:edit")
    public AjaxResult editUser(Smalluser user){
        if(userService.checkPhoneUnique(user))
        {
            return toAjax(userService.editUser(user));
        }
        else
            return error("更新机构用户'"+user.getSuName()+"'失败，联系方式已存在");
    }

    //    删除用户
    @PostMapping("/remove/{suId}")
    @RequiresPermissions("itsystem:smalluser:remove")
    @Log(title = "机构用户管理",businessType = BusinessType.DELETE)
    @ResponseBody
    public AjaxResult removeUser(@PathVariable("suId") Long suId){
        boolean bl = userService.removeUser(suId);
        if (bl){
            return success("删除成功！");
        }else {
        return error("删除失败！");
        }
}

}
